﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace dbComponentGenerator
{
    public partial class frmMainForm : Form
    {
        public frmMainForm()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = fbd.SelectedPath + (fbd.SelectedPath[fbd.SelectedPath.Length-1] == '\\'?"":"\\").ToString();
                btnGenerate.Enabled = true;
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {

            string generatedPath = txtFolder.Text + "Generated\\";
            if (!Directory.Exists(generatedPath))
                Directory.CreateDirectory(generatedPath);
            
            if (txtTableName.Text == "")
            {
                MessageBox.Show("يجب تحديد جدول لتوليد ملفاته");
                return;
            }


            if (txtConnection.Text == "")
            {
                MessageBox.Show("يجب تحديد اتصال لقاعدة بيانات");
                return;
            }


            if (txtProvider.Text == "")
            {
                MessageBox.Show("يجب تحديد البروفايدر علي سبيل المثال ما بين القوسين (System.Data.SqlClient)");
                return;
            }

            try
            {
                if (txtNameSpace.Text.Trim() == "")
                {
                    dbComponentGenerator.GlobalVars.strNameSpace = "DatabaseComponent";

                }
                else
                {
                    dbComponentGenerator.GlobalVars.strNameSpace = txtNameSpace.Text.Trim();

                    dbComponentGenerator.GlobalVars.strNameSpace = System.Text.RegularExpressions.Regex.Replace(dbComponentGenerator.GlobalVars.strNameSpace, "[^a-zA-Z0-9.]", "");

                    if (dbComponentGenerator.GlobalVars.strNameSpace == "")
                    {
                        dbComponentGenerator.GlobalVars.strNameSpace = "DatabaseComponent";
                    }
                }



                string strRet = dbComponentGenerater.GenerateTableClass(txtTableName.Text, txtConnection.Text, txtProvider.Text);
                File.WriteAllText(generatedPath + txtTableName.Text.Replace(" ", "_") + ".generated.cs", strRet);


                strRet = dbComponentGenerater.GenerateTableAdapter(txtTableName.Text, txtConnection.Text, txtProvider.Text);
                File.WriteAllText(generatedPath + txtTableName.Text.Replace(" ", "_") + "DB" + ".generated.cs", strRet);


                strRet = dbComponentGenerater.GenerateTableClassEmpty(txtTableName.Text, txtConnection.Text, txtProvider.Text);
                File.WriteAllText(txtFolder.Text + txtTableName.Text.Replace(" ", "_") + ".cs", strRet);


                strRet = dbComponentGenerater.GenerateTableAdapterEmpty(txtTableName.Text, txtConnection.Text, txtProvider.Text);
                File.WriteAllText(txtFolder.Text + txtTableName.Text.Replace(" ", "_") + "DB" + ".cs", strRet);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            MessageBox.Show("تم توليد الملفات بنجاح");

            
        }

        private void frmMainForm_Load(object sender, EventArgs e)
        {
            try
            {
                txtConnection.Text = ConfigurationManager.ConnectionStrings["mainconn"].ConnectionString;
                txtProvider.Text = ConfigurationManager.ConnectionStrings["mainconn"].ProviderName;
            }
            catch
            {
                txtProvider.Text = "System.Data.SqlClient";
            }

            try
            {
                txtFolder.Text = ConfigurationManager.AppSettings["defaultPath"];
                if (Directory.Exists(txtFolder.Text))
                {
                    btnGenerate.Enabled = true;
                }
            }
            catch
            {
            }

            try
            {
                txtTableName.Text = ConfigurationManager.AppSettings["table"];
            }
            catch
            {
            }


        }
    }
}
